﻿<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,initial-scale=1.0,user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <title>编辑角色</title>
    <link rel="stylesheet" href="~/lib/element-ui/theme-chalk/index.css">
    <link rel="stylesheet" href="~/admin/css/default/style.css">
</head>

<body class="mainbody">
    <div id="app" v-cloak>
        <div class="location">
            <el-breadcrumb separator-class="el-icon-arrow-right">
                <el-breadcrumb-item>
                    <i class="el-icon-s-home"></i>
                    管理中心
                </el-breadcrumb-item>
                <el-breadcrumb-item>首页</el-breadcrumb-item>
                <el-breadcrumb-item>系统管理</el-breadcrumb-item>
                <el-breadcrumb-item>编辑角色</el-breadcrumb-item>
            </el-breadcrumb>
        </div>

        <div class="content-box">
            <el-tabs v-model="activeName" type="card">
                <el-tab-pane label="基本信息" name="info">
                    <div class="tab-content">
                        <el-form ref="editForm1" :model="model" :rules="rules" :label-position="labelPosition" label-width="120px">
                            <el-form-item label="角色类型">
                                <el-select v-model="model.roleType" placeholder="请选择...">
                                    <el-option label="系统管理员" :value="1"></el-option>
                                    <el-option label="超级管理员" :value="2"></el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item prop="name" label="角色标识">
                                <el-input v-model="model.name" placeholder="唯一名称，不可重复"></el-input>
                            </el-form-item>
                            <el-form-item prop="title" label="角色名称">
                                <el-input v-model="model.title" placeholder="允许中文,128字符内"></el-input>
                            </el-form-item>
                            <el-form-item label="分配权限" v-if="model.roleType===1">
                                <el-card>
                                    <el-tree :data="model.navigation" node-key="id" default-expand-all :expand-on-click-node="false">
                                        <span class="tree-node" slot-scope="{ node, data }">
                                            <span>{{ data.title }}</span>
                                            <span class="tools-box">
                                                <el-checkbox size="mini" v-for="item in data.resource" v-model="item.isSelected" :key="item.name">{{item.title}}</el-checkbox>
                                            </span>
                                        </span>
                                    </el-tree>
                                </el-card>
                            </el-form-item>
                        </el-form>
                    </div>
                </el-tab-pane>
            </el-tabs>
        </div>

        <div class="footer-box">
            <div class="footer-btn">
                <el-button type="primary" @@click="submitForm">确认保存</el-button>
                <el-button plain @@click="resetForm">取消返回</el-button>
            </div>
        </div>
    </div>

    <script src="~/lib/vue/vue.js"></script>
    <script src="~/lib/element-ui/index.js"></script>
    <script src="~/lib/axios/axios.min.js"></script>
    <script src="~/lib/babel-polyfill/polyfill.min.js"></script>
    <script src="~/admin/js/axios.config.js"></script>
    <script>
        //初始化VUE
        var ve = new Vue({
            el: '#app',
            data: {
                activeName: 'info',//当前选项卡
                labelPosition: 'right',//表单对齐方式
                model: {
                    id: 0,
                    name: null,
                    title: null,
                    roleType: null,
                    isSystem: 0,
                    navigation: []
                },
                rules: {
                    name: [
                        { required: true, message: '请输入角色标识', trigger: 'blur' },
                        { min: 2, max: 128, message: '长度在 2 到 128 个字符', trigger: 'blur' },
                        { pattern: /^[0-9a-zA-Z_]{1,}$/, message: '只能字母数字下划线', trigger: 'blur' }
                    ],
                    title: [
                        { required: true, message: '请输入角色标题', trigger: 'blur' },
                    ],
                    roleType: [
                        { required: true, message: '请选择角色类型', trigger: 'change' }
                    ]
                }
            },
            methods: {
                //初始化数据
                initData: function () {
                    let _this = this; //当前页面
                    let id = GetQueryValue("id"); //获取ID
                    if (id > 0) {
                        AxiosAjax({
                            url: '/manager/role/' + id,
                            loading: true,
                            success: function (res) {
                                //赋值给model
                                Object.keys(_this.model).forEach(key => { _this.model[key] = res.data[key] });
                                //console.log(_this.model);
                            }
                        });
                    } else {
                        AxiosAjax({
                            url: '/manager/role/navigation',
                            loading: true,
                            success: function (res) {
                                //赋值给model菜单
                                _this.model.navigation = res.data;
                                //console.log(_this.model.navigation);
                            }
                        });
                    }
                },
                submitForm: function () {
                    let _this = this; //当前页面
                    //多个表单验证
                    const f1 = new Promise((resolve, reject) => {
                        _this.$refs['editForm1'].validate((valid) => {
                            if (valid) resolve()
                        });
                    });
                    Promise.all([f1]).then(() => {
                        //提交表单,Id大于0则修改，否则添加
                        if (_this.model.id > 0) {
                            AxiosAjax({
                                method: 'put',
                                url: 'manager/role/' + _this.model.id,
                                data: _this.model,
                                loading: true,
                                successMsg: '修改管理角色已成功',
                                success: function (res) {
                                    //可跳转加列表页
                                    _this.linkUrl('rolelist');
                                }
                            });
                        } else {
                            AxiosAjax({
                                method: 'post',
                                url: '/manager/role',
                                data: _this.model,
                                loading: true,
                                successMsg: '新增管理角色已成功',
                                success: function (res) {
                                    //重置表单
                                    _this.$refs['editForm1'].resetFields();
                                }
                            });
                        }
                    })
                },
                //跳转链接
                linkUrl: function (url) {
                    window.location.href = url;
                },
                resetForm() {
                    window.history.back(-1);
                }
            },
            created: function () {
                //初始化数据
                this.initData();
            }
        })
    </script>
</body>
</html>
